package com.krain.ddbb.entity;

import android.app.Application;
import android.content.Context;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;

import com.krain.corelibrary.entity.PubImageBean;
import com.krain.corelibrary.net.NetClient;
import com.krain.ddbb.base.BaseActivity;
import com.krain.ddbb.constant.Keys;
import com.krain.ddbb.util.HttpUtil;

import java.io.File;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ex-kuangxiaomin101 on 2015-12-11.
 */
public class AppApi extends HttpUtil {

    private static AppApi sInstance = null;

    public AppApi(Context context) {
        super(context);
    }


    public synchronized static AppApi getsInstance(Context context) {
        if (sInstance == null) {
            sInstance = new AppApi(context.getApplicationContext());
        }
        return sInstance;
    }


    /**
     * 获取用户验证码
     *
     * @param mobile
     * @return
     */
    public CaptchaBean getAuthCode(String mobile) {
        String methodUrl = "/v1/user/send-code";
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        map.put(Keys.KEY_MOBILE, mobile);
        return post(CaptchaBean.class, methodUrl, map);
    }


    /**
     * 注册/登录
     *
     * @param phone
     * @param code
     * @return
     */
    public UserInfoBean login(String phone, String code) {
        String methodUrl = "/v1/user/create";
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        map.put("phone", phone);
        map.put("code", code);
        return post(UserInfoBean.class, methodUrl, map);
    }


    /**
     * 我的信息页面
     *
     * @param token
     * @return
     */
    public UserInfoBean getUserInfo(String token) {
        String methodUrl = new StringBuffer("/v1/user-extra/detail?").append(Keys.KEY_TOKEN).append("=").append(token).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        return get(UserInfoBean.class, methodUrl, map);
    }


    /**
     * 获取工种列表
     *
     * @param token
     * @return
     */
    public List<WorkNameBean> getWorkList(String token) {
        String methodUrl = new StringBuffer("/v1/work-type/index?").append(Keys.KEY_TOKEN).append("=").append(token).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        return getList(WorkNameBean.class, methodUrl, map).getList();
    }

    /**
     * 获取地址列表
     *
     * @param token
     * @return
     */
    public List<PersonArdessBean> getAddressList(String token) {
        String methodUrl = new StringBuffer("/v1/address/index?").append(Keys.KEY_TOKEN).append("=").append(token).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        return getList(PersonArdessBean.class, methodUrl, map).getList();
    }

    /**
     * 我发布的订单列表
     *
     * @param token
     * @return
     */
    public BaseListEntity<UserIssueBean> getIssueList(String token, int page) {
        String methodUrl = new StringBuffer("/v1/order/index?").append(Keys.KEY_TOKEN).append("=").append(token).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        map.put("page", String.valueOf(page));
        return getList(UserIssueBean.class, methodUrl, map);
    }


    /**
     * 对我的评价列表
     *
     * @param token
     * @return
     */
    public BaseListEntity<ServiceRmarkBean> getRmarkList(String token, int page) {
        String methodUrl = new StringBuffer("/v1/evaluate/index?").append(Keys.KEY_TOKEN).append("=").append(token).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        map.put("page", String.valueOf(page));
        return getList(ServiceRmarkBean.class, methodUrl, map);
    }

    /**
     * 我服务的订单列表
     *
     * @param token
     * @return
     */
    public BaseListEntity<UserIssueBean> getServerList(String token, int page) {
        String methodUrl = new StringBuffer("/v1/order/server-index?").append(Keys.KEY_TOKEN).append("=").append(token).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        map.put("page", String.valueOf(page));
        return getList(UserIssueBean.class, methodUrl, map);
    }

    /**
     * 收支明细列表
     *
     * @param token
     * @return
     */
    public BaseListEntity<AccountDetailBean> getAccountList(String token, int page) {
        String methodUrl = new StringBuffer("/v1/user-extra/with-draw-list?").append(Keys.KEY_TOKEN).append("=").append(token).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        map.put("page", String.valueOf(page));
        return getList(AccountDetailBean.class, methodUrl, map);
    }


    /**
     * 获取工种类型
     *
     * @param token
     * @return
     */
    public OrderDetailTypeBean getWorktype(String token) {
        String methodUrl = new StringBuffer("/v1/order/detail?").append(Keys.KEY_TOKEN).append("=").append(token).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        return get(OrderDetailTypeBean.class, methodUrl, map);
    }

    /**
     * (修改/保存)我的信息
     */
    public UserInfoBean setUserInfo(String sToken, String username, String picture, String front_image, String opposite_image) {
        String methodUrl = new StringBuffer("/v1/user-extra/create?").append(Keys.KEY_TOKEN).append("=").append(sToken).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        map.put("username", username);
        if (!TextUtils.isEmpty(front_image))
            map.put("front_image", front_image);
        if (!TextUtils.isEmpty(front_image))
            map.put("opposite_image", opposite_image);
        if (!TextUtils.isEmpty(picture))
            map.put("picture", picture);
        return post(UserInfoBean.class, methodUrl, map);
    }

    /**
     * 增加用户地址
     */
    public BaseEntity postAddress(String sToken, String address) {
        String methodUrl = new StringBuffer("/v1/address/create?").append(Keys.KEY_TOKEN).append("=").append(sToken).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        map.put("address", address);
        return post(BaseEntity.class, methodUrl, map);
    }


    /**
     * 上传图片
     */
    public UpLoadBean postFile(String sToken, File file) {
        String methodUrl = new StringBuffer("/v1/image/upload").toString();
        ArrayList<File> files = new ArrayList<>();
        files.add(file);
        List<UpLoadBean> beans = uploadFile(files, methodUrl, sToken);
        if (beans != null && beans.size() > 0)
            return beans.get(0);
        else return null;
    }

    /**
     * 上传图片
     */
    public List<UpLoadBean> postFiles(String sToken, ArrayList<File> files) {
        String methodUrl = new StringBuffer("/v1/image/upload").toString();
        return uploadFile(files, methodUrl, sToken);
    }


    /**
     * 上传经纬度
     *
     * @param sToken
     * @param lat
     * @param lng
     * @return
     */
    public BaseEntity upLoadLocation(String sToken, double lat, double lng) {
        String methodUrl = new StringBuffer("/v1/user-position/create?").append(Keys.KEY_TOKEN).append("=").append(sToken).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        map.put("longitude", String.valueOf(lng));
        map.put("latitude", String.valueOf(lat));
        return post(BaseEntity.class, methodUrl, map);
    }

    /**
     * 订单提交
     *
     * @return
     */
    public BaseEntity postOrder(String sToken, int work_id, String server_time, String server_price, int server_address, List<UpLoadBean> order_image, String desc) {
        String methodUrl = new StringBuffer("/v1/order/create?").append(Keys.KEY_TOKEN).append("=").append(sToken).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        map.put("work_id", String.valueOf(work_id));
        map.put("server_time", String.valueOf(server_time));
        map.put("server_price", String.valueOf(server_price));
        map.put("server_address", String.valueOf(server_address));
        StringBuffer buffer = new StringBuffer();
        for (UpLoadBean bean : order_image) {
            buffer.append(bean.getImage());
            if (order_image.indexOf(bean) != order_image.size() - 1) {
                buffer.append(",");
            }
        }
        map.put("order_image", buffer.toString());
        map.put("desc", String.valueOf(desc));
        return post(BaseEntity.class, methodUrl, map);
    }


    /**
     * 订单详情页
     *
     * @param sToken
     * @param orderId
     * @return
     */
    public UserIssueBean getOrderDetail(String sToken, int orderId) {
        String methodUrl = new StringBuffer("/v1/order-pay/view?").append(Keys.KEY_TOKEN).append("=").append(sToken).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        map.put("order_id", String.valueOf(orderId));
        return get(UserIssueBean.class, methodUrl, map);
    }


    /**
     * 用户确认服务
     *
     * @param sToken
     * @param orderId
     * @return
     */
    public BaseEntity confirmServer(String sToken, int orderId) {
        String methodUrl = new StringBuffer("/v1/order-pay/confirm?").append(Keys.KEY_TOKEN).append("=").append(sToken).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        map.put("order_id", String.valueOf(orderId));
        return post(BaseEntity.class, methodUrl, map);
    }


    /**
     * 用户重新下单
     *
     * @param sToken
     * @param orderId
     * @return
     */
    public BaseEntity publishOrder(String sToken, int orderId) {
        String methodUrl = new StringBuffer("/v1/order-pay/re-order?").append(Keys.KEY_TOKEN).append("=").append(sToken).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        map.put("order_id", String.valueOf(orderId));
        return post(BaseEntity.class, methodUrl, map);
    }


    /**
     * 用户提现页面的展示
     *
     * @param sToken
     * @return
     */
    public UserAccountBean userAccount(String sToken) {
        String methodUrl = new StringBuffer("/v1/user-extra/with-draw-index?").append(Keys.KEY_TOKEN).append("=").append(sToken).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        return get(UserAccountBean.class, methodUrl, map);
    }


    /**
     * 用户提现
     *
     * @param sToken
     * @return
     */
    public BaseEntity widthDraw(String sToken,String money) {
        String methodUrl = new StringBuffer("/v1/user-extra/with-draw?").append(Keys.KEY_TOKEN).append("=").append(sToken).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        map.put("money",money);
        return post(BaseEntity.class, methodUrl, map);
    }


    /**
     * 更新工种信息
     *
     * @param sToken
     * @return
     */
    public BaseEntity updateWorkerMsg(String sToken, List<String> list) {
        String methodUrl = new StringBuffer("/v1/user-work/create?").append(Keys.KEY_TOKEN).append("=").append(sToken).toString();
        StringBuffer buffer = new StringBuffer();
        for (String str : list) {
            buffer.append(str);
            buffer.append(",");
        }
        buffer.deleteCharAt(buffer.length() - 1);
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        map.put("work_id", buffer.toString());
        return post(BaseEntity.class, methodUrl, map);
    }


    /**
     * 抢单的接口
     *
     * @param sToken
     * @param order_id
     * @return
     */
    public RobOrderBean getRobOrderMsg(String sToken, String order_id) {
        String methodUrl = new StringBuffer("/v1/order/view?").append(Keys.KEY_TOKEN).append("=").append(sToken).toString();
        ArrayMap<String, String> arrayMap = new ArrayMap<String, String>();
        arrayMap.put("order_id", order_id);
        return get(RobOrderBean.class, methodUrl, arrayMap);
    }


    /**
     * 抢单
     *
     * @param sToken
     * @param orderId
     * @return
     */
    public BaseEntity robOrdeer(String sToken, String orderId) {
        String methodUrl = new StringBuffer("/v1/order-pay/create?").append(Keys.KEY_TOKEN).append("=").append(sToken).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        map.put("order_id", orderId);
        return post(BaseEntity.class, methodUrl, map);
    }


    /**
     * 评论
     *
     * @param sToken
     * @param orderId
     * @return
     */
    public BaseEntity comment(String sToken, String comment, int r1, int r2, int r3, String orderId) {
        String methodUrl = new StringBuffer("/v1/evaluate/create?").append(Keys.KEY_TOKEN).append("=").append(sToken).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        map.put("order_id", orderId);
        map.put("technology_score", String.valueOf(r1));
        map.put("time_score", String.valueOf(r2));
        map.put("polite_score", String.valueOf(r3));
        map.put("desc", comment);
        return post(BaseEntity.class, methodUrl, map);
    }

    /**
     * 删除银行卡
     * @param sToken
     * @param bankId
     * @return
     */
    public BaseEntity unBindBank(String sToken, int bankId) {
        String methodUrl = new StringBuffer("/v1/bank/delete?").append(Keys.KEY_TOKEN).append("=").append(sToken).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        map.put("bank_id", String.valueOf(bankId));
        return post(BaseEntity.class, methodUrl, map);
    }

    /**获取用户银行卡
     *
     * @param sToken
     * @return
     */
    public UserCardBean getUserCardMsg(String sToken){
        String methodUrl = new StringBuffer("/v1/bank/index?").append(Keys.KEY_TOKEN).append("=").append(sToken).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        return get(UserCardBean.class, methodUrl, map);
    }

    /**
     * 绑定新的银行卡
     * @param sToken
     * @param num
     * @param fullname
     * @return
     */
    public BaseEntity bindUserCard(String sToken,String num,String fullname){
        String methodUrl = new StringBuffer("/v1/bank/create?").append(Keys.KEY_TOKEN).append("=").append(sToken).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        map.put("bank_number",num);
        map.put("bank_fullname",fullname);
        return post(BaseEntity.class, methodUrl, map);
    }


    /**
     * 确认服务完成
     * @param sToken
     * @return
     */
    public BaseEntity confirmServerComplete(String sToken,String oid){
        String methodUrl = new StringBuffer("/v1/order-pay/confirm-service?").append(Keys.KEY_TOKEN).append("=").append(sToken).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        map.put("order_id",oid);
        return post(BaseEntity.class, methodUrl, map);
    }

    /**
     * 删除用户地址
     * @param sToken
     * @return
     */
    public BaseEntity deleteUserAddress(String sToken,String aid){
        String methodUrl = new StringBuffer("/v1/address/delete?").append(Keys.KEY_TOKEN).append("=").append(sToken).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        map.put("id",aid);
        return post(BaseEntity.class, methodUrl, map);
    }

    /**
     * 获取支付的信息
     * @param sToken
     * @param oid
     * @param type
     * @return
     */
    public String getPayMsg(String sToken,String oid,String type){
        String methodUrl = new StringBuffer("/v1/order-pay/pay?").append(Keys.KEY_TOKEN).append("=").append(sToken).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        map.put("order_id",oid);
        map.put("type",type);
        return NetClient.getInstance(context).postResult(methodUrl,map);
    }


    /**
     * 获取抢单列表
     * @param lng
     * @param lat
     * @param page
     * @return
     */
    public BaseListEntity<RobOrderBean> getUnRobOrderList(String sToken,String lng,String lat,int page){
        String methodUrl = new StringBuffer("/v1/order/order-list?").append(Keys.KEY_TOKEN).append("=").append(sToken).toString();
        ArrayMap<String, String> map = new ArrayMap<String, String>();
        map.put("longitude",lng);
        map.put("latitude",lat);
        map.put("page",String.valueOf(page));
        return getList(RobOrderBean.class, methodUrl, map);
    }
}
